<?php
// Library File
include 'lib/library.php';

// URL address
$addr = address();

// Local address
$rel_addr = relative_address();

// Database Connection
include $rel_addr.'/lib/mysql_connect.inc';

// Header File
include $rel_addr.'/lib/header.php';

// Footer File
include $rel_addr.'/lib/footer.php';


$title = 'Search';
$subtitle = 'Search for items';

$found = false;

$item = $_GET['item'];

if(!isset($item)){
	$content .= '
		<div class="formfield search">
	';
		include $rel_addr.'/handle/search_handle.php';
		
		if(!$found){
			$content .= '
				<div class="formcontent whole">
				<div class="formhelp search">
				<h3>Tooltips</h3>
				</div>
				<div class="formcontent search">
					<form action="'.$addr.this_page().'" method="POST">
					
					<div>
					<P>
						<input type="text" size="20" id="search" name="search" /> <input type="submit" name="submit" value="Search" />
						<span class="formhelp">
						<P>
						Check some of the boxes to left to more clearly define your search. The default setting is to search for items in all areas donated by all types of members.
						</P>
						</span>
					</P>
					
					<br /><br /><br />
					
					<P>
						<label for="tags">Search Tags: </label><br />
						<textarea cols="30" rows="2" id="tags" name="tags"></textarea>
						<span class="formhelp">
						<P>
						Add tags to search for specific items. Insert a comma(,) between different tags. I.E. computer, Printer, office supplies.
						</P>
						</span>
					</P>
					
					</div>
						
					<br />
					</form>
				</div>
				</div>
				
			';
		}
			
	$content .= '
		</div>
	';

// An item is being looked at
}else if(isset($item)){
	$query = mysql_query("SELECT * FROM items WHERE item_id='$item'");
	$item_exists = mysql_num_rows($query);
	
	$content .= '
		<div class="itembox">
	';
	
	// The item_id exists
	if($item_exists){
		$item_array = mysql_fetch_assoc($query);
		
		// get name of user that donated item
		$query = mysql_query("SELECT name FROM users WHERE user_id='{$item_array['user_id']}'");
		$user_array = mysql_fetch_assoc($query);
		$name = $user_array['name'];
		
		// Get tag_ids related to item
		$tag_ids;
		$query = mysql_query("SELECT tag_id FROM tagmap WHERE item_id='$item'");
		while($tag_ids_array = mysql_fetch_array($query)){
			$tag_ids[] = $tag_ids_array['tag_id'];
		}
		
		// Get names of the tag_ids related to the item
		$tag_names;
		$tag_ids_count = count($tag_ids);
		for($x=0;$x < $tag_ids_count;$x++){
			$query = mysql_query("SELECT name FROM tags WHERE tag_id='{$tag_ids["$x"]}'");
			$tag_array = mysql_fetch_assoc($query);
			$tag_names[] = $tag_array['name'];
		}
		
		$content .= '
			<div class="image">
				<img src="'.$addr.'/images/'.$item_array['img_src'].'" alt="'.$item_array['img_src'].'" />
			</div>
			
			<div class="data name">
				<P>'.stripslashes($item_array['name']).'</P>
			</div>
			
			<div class="data donor">
				<P>Donated By: <a href="'.$addr.'/profile.php?id='.$item_array['user_id'].'">'.$name.'</a></P>
			</div>
			
			<div class="data description">
				<P>'.nl2br(stripslashes($item_array['description'])).'</P>
			</div>
			
			<div class="tags">
				<P><img src="'.$addr.'/imgs/tag.png" />Tags: 
		';
				$tag_names_count = count($tag_names);
				for($x=0;$x < $tag_names_count;$x++){
					$content .= ''.$tag_names["$x"].'';
					if($x < $tag_names_count - 1){
						$content .= ', ';
					}
				}
			
		$content .= '
				</P>
			</div>
			
			<div class="contact">
				<P><a href="'.$addr.'/messages.php?mode=compose&to='.$name.'&subject=About the '.stripslashes($item_array['name']).'">Contact</a></P>
			</div>
		';
		
	// The item_id doesn't exist
	}else if(!$item_exists){
		$content .= '
			<P> That item does not exist. </P>
		';
		refresh();
	}
	
	$content .= '
		</div>
	';
}

if(isset($_POST['submit']) && $found){

	$side = '
		<ul>
		<li><a href="search.php">New Search</a></li>
		</ul>
	';
}else {
	$side = '
	
	';
}
/*
**   P U T    V A R S
**    O N    P A G E
*/
	top($title,$style);
	middle($header,$subtitle,$content,$side);
	bottom($navigation,$footer,$dbc);
	
?>
